//
//  UIImage+ImageProcess.m
//  LAMapBasicDemo
//
//  Created by 罗亮富 on 2018/2/8.
//  Copyright © 2018年 2Bulu. All rights reserved.
//

#import "UIImage+Ulti.h"

@implementation UIImage (Ulti)
- (UIImage*)imageRotatedByDegrees:(CGFloat)degrees
{
    CGFloat rotation = degrees * M_PI / 180;
    return [self imageRotatedByRadians:rotation];
}

- (UIImage*)imageRotatedByRadians:(CGFloat)rotation
{
    // Calculate Destination Size
    CGAffineTransform t = CGAffineTransformMakeRotation(rotation);
    CGRect sizeRect = (CGRect) {.size = self.size};
    CGRect destRect = CGRectApplyAffineTransform(sizeRect, t);
    CGSize destinationSize = destRect.size;
    
    // Draw image
    UIGraphicsBeginImageContext(destinationSize);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextTranslateCTM(context, destinationSize.width / 2.0f, destinationSize.height / 2.0f);
    CGContextRotateCTM(context, rotation);
    [self drawInRect:CGRectMake(-self.size.width / 2.0f, -self.size.height / 2.0f, self.size.width, self.size.height)];
    
    // Save image
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}
@end
